#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
 
def send_mail(receiver='18910158363@163.com', attach="main.py"):

    receivers = [receiver] 

    sender = '18910158363@163.com'
    mail_host="smtp.163.com"
    mail_user="18910158363"
    mail_pass="aa11bb22"
 
    message = MIMEMultipart()
    message['From'] = formataddr(["核酸检测", sender])
    message['To'] = formataddr(["客人", receiver])
 
    subject = '你的发票信息'
    message['Subject'] = Header(subject, 'utf-8')
    message.attach(MIMEText('附件为发票信息请查收', 'plain', 'utf-8'))

    att1 = MIMEText(open('mail.py', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="invoice.pdf"'
    message.attach(att1)
 
    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 25)
        smtpObj.login(mail_user,mail_pass)  
        smtpObj.sendmail(sender, receivers, message.as_string())
        print ("邮件发送成功")
        return 0
    except smtplib.SMTPException as e:
        print ("Error: 无法发送邮件",e)
        return -1
 
if __name__ == "__main__":
    send_mail(receiver="xiechc@gmail.com")

